﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace Infrastructure.ApiCache
{
    /// <summary>
    /// 用于Api接口缓存多少秒
    /// </summary>
    public class HttpApiCacheForTimeSpanAttribute : Attribute
    {
        public int Second { get; set; }
        public int Minute { get; set; }
        public bool ByUser { get; set; }
        /// <summary>
        /// 默认按秒存
        /// </summary>
        /// <param name="Second">分钟/秒 清空缓存 二秒一</param>
        /// <param name="Minute">小时/分钟 清空缓存  二秒一</param>
        /// <param name="ByUser">是否按用户维度</param>
        public HttpApiCacheForTimeSpanAttribute(int Second = 0, int Minute = 0, bool ByUser = false)
        {
            this.Second = Second;
            this.Minute = Minute;
            this.ByUser = ByUser;
        }

        public DateTime GetNextCacheTime()
        {
            if (Second>0)
            {
                return DateTime.Now.AddSeconds(Second- DateTime.Now.Second % Second);
            }
            else
            {
                return DateTime.Now.AddMinutes(Minute - DateTime.Now.Minute % Minute).AddSeconds(1-DateTime.Now.Second);
            }
        }
    }
}
